/*
    mathtools, a calculator-complimentary application for math studies
    Copyright (C) 2009 Irving Axel Jounior Rivas Zarete

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    If you need to contact me, please do so: axel (period) rivas (inside) gmail (dot) com
*/
#include <stdio.h>
#include "mathwork.c"

/*
Function declarations.
	void uiesprimo();
		UserInterface to manage "int esprimo(int num)", found in mathwork.c
	void uifprimos();
		UserInterface to manage "int *fprimos(int num)", found in mathwork.c
	char menu();
		Shows the menu and returns selected valid option.
*/
void uiesprimo();
void uifprimos();
char menu();

void uiesprimo(){
	int n;
	do{		printf("Digite el numero para saber si es primo.\n");		printf("Para finalizar introduzca un numero no positivo.\n");		scanf("%d",&n);		if( esprimo(n) == 1 && (n > 1) ){			printf("El numero %d es primo.\n",n);		}else if (n>0) printf("El numero %d no es primo.\n",n);				}while(n >= 1);
	return;
}

void uifprimos(){
	int n,*pt,i;
	do{
		printf("Para finalizar, introduzca un numero no positivo.\n");
		printf("Digite el numero del cual desea conocer los factores primos:\n");
		scanf("%d",&n);
		if(n < 1)
			break;
		if (n == 1)
			printf("Se necesita un numero mayor o igual a dos.");
		if(n  >1)		
			pt = fprimos(n);
		//fprimos returns a pointer to the prime numbers. the first number is the amount of
		//elements allocated in the pointer
		i = pt[0];
		printf("EL NUMERO DE FACTORES ES: %d\n\n\n",i);
		printf("A continuación la lista de factores primos:\n");
		for(;i >= 1;i--){
			printf("%d\n",pt[i]);
		}
	}while(n > 0);
	return;
}

char menu(){
	char opcion = '.';
	//Menu, option selection.
	while( ((opcion < 'A') || (opcion > 'C')) && ((opcion < 'a') || (opcion > 'c')) ){
		printf("--Menu--\nseleccione con la letra de la opcion.\n\na)Factores Primos\nb)Saber si un numero es primo\nc)Salir\n");
		scanf("%c",&opcion);
	}
	return opcion;
}
